﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

using VisionDesigner;
using VisionDesigner.CircleFind;
using MvdXmlParse;
using VisionDesigner.Camera;
using GlobalCameraModuleCs;
using ImageSourceModuleCs;

using VM.Core;
using VM.PlatformSDKCS;
using VMControls.Interface;
using VMControls.RenderInterface;
using VMControls.Winform.Release;
using Mvvm.Commands;
using GlobalCameraModuleCs;
using ImageSourceModuleCs;
using IMVSCircleFindModuCs;
using IMVSLineFindModuCs;
using IMVSCaliperCornerModuCs;
using IMVSFastFeatureMatchModuCs;
using IMVSHPFeatureMatchModuCs;
using VisionDesigner;
using VisionDesigner.HPFeaturePatMatch;
using VisionDesigner.FastFeaturePatMatch;

using Vision.src;
using Vision.roi;
using Vision.page;


namespace Vision.page
{
    public partial class CameraControl : UserControl
    {

        private uint m_nDeviceNum = 0;
        //相机相关变量
        private VisionDesigner.Camera.CCameraTool m_cMyCameraToolObj = null;

        private GlobalCameraModuleTool globalTool = null;

        public CameraControl()
        {
            InitializeComponent();
        }

        public CameraControl(string FeatureModuleName)
        {
            InitializeComponent();

            this.comboBox2.Items.Add("Mono 8");
            this.comboBox2.Items.Add("RGB 24 C3");

            VmProcedure vmProcess = (VmProcedure)VmSolution.Instance[Form1.MyForm.ModuleProcessName];
            //获取流程中所有全局相机模块
            List<GlobalCameraModuleTool> glCameralist = new List<GlobalCameraModuleTool>();
            List<VmModule> vmModules = new List<VmModule>();
            VmSolution.Instance.GetAllModule(vmModules);

            comboBox1.Items.Clear();
            foreach (var module in vmModules.OfType<GlobalCameraModuleTool>())
            {
                glCameralist.Add(module);
                comboBox1.Items.Add(module.Name);
            }
            comboBox1.Text = glCameralist[0].Name;

            ImageSourceModuleTool ImageSourceModule = (ImageSourceModuleTool)VmSolution.Instance[FeatureModuleName];
            ImageSourceModule.ModuParams.ImageSourceType = ImageSourceParam.ImageSourceTypeEnum.Camera;
            ImageSourceModule.ModuParams.SetParamValue("CameraID", glCameralist[0].ID.ToString());

            // 创建设备列表
            System.GC.Collect();
            cbDeviceList.Items.Clear();
            try
            {
                m_nDeviceNum = CCameraTool.EnumDevices(Convert.ToUInt32(VisionDesigner.Camera.MVD_TRANSFER_LAYER_TYPE.MVD_GIGE_DEVICE) | Convert.ToUInt32(VisionDesigner.Camera.MVD_TRANSFER_LAYER_TYPE.MVD_USB_DEVICE));

                // 在窗体列表中显示设备名
                foreach (var item in CCameraTool.DeviceInfoList)
                {
                    VisionDesigner.Camera.CDeviceInfo cDeviceInfo = item as VisionDesigner.Camera.CDeviceInfo;
                    if (cDeviceInfo is CGigeDeviceInfo)
                    {
                        CGigeDeviceInfo cGigeDeviceInfo = cDeviceInfo as CGigeDeviceInfo;
                        if (cGigeDeviceInfo.UserDefinedName != "")
                        {
                            cbDeviceList.Items.Add(cGigeDeviceInfo.SerialNumber);
                        }
                        else
                        {
                            cbDeviceList.Items.Add(cGigeDeviceInfo.SerialNumber);
                        }
                    }
                    else if (cDeviceInfo is CUSBDeviceInfo)
                    {
                        CUSBDeviceInfo cUsbDeviceInfo = cDeviceInfo as CUSBDeviceInfo;

                        if (cUsbDeviceInfo.UserDefinedName != "")
                        {
                            cbDeviceList.Items.Add(cUsbDeviceInfo.SerialNumber);
                        }
                        else
                        {
                            cbDeviceList.Items.Add(cUsbDeviceInfo.SerialNumber);
                        }
                    }
                }

                // 选择第一项
                if (m_nDeviceNum != 0)
                {
                    cbDeviceList.SelectedIndex = 0;
                }

                if (m_nDeviceNum == 0 || cbDeviceList.SelectedIndex == -1)
                {
                    Form1.MyForm.SetLabelText("No device!\r\n");
                    return;
                }
            }
            catch (MvdException ex)
            {
                Form1.MyForm.SetLabelText("Fail to enum devices! ErrorCode: 0x" + ex.ErrorCode.ToString("X") + "\r\n");
            }

            globalTool = VmSolution.Instance[comboBox1.Text] as GlobalCameraModuleTool;
            globalTool.ModuParams.SetChosenCameraSN(cbDeviceList.Text);
            if (globalTool.ModuParams.PixelFormat == (int)VMPixelFormat.VM_PIXEL_MONO_08)
            {
                this.comboBox2.Text = "Mono 8";
            }
            else if (globalTool.ModuParams.PixelFormat == (int)VMPixelFormat.VM_PIXEL_RGB24_C3)
            {
                this.comboBox2.Text = "RGB 24 C3";
            }
            else
            {
                globalTool.ModuParams.PixelFormat = (int)VMPixelFormat.VM_PIXEL_RGB24_C3;
                this.comboBox2.Text = "RGB 24 C3";
            }

        }

        private void bnEnum_Click(object sender, EventArgs e)
        {
            // 创建设备列表
            System.GC.Collect();
            cbDeviceList.Items.Clear();
            try
            {
                m_nDeviceNum = CCameraTool.EnumDevices(Convert.ToUInt32(VisionDesigner.Camera.MVD_TRANSFER_LAYER_TYPE.MVD_GIGE_DEVICE) | Convert.ToUInt32(VisionDesigner.Camera.MVD_TRANSFER_LAYER_TYPE.MVD_USB_DEVICE));

                // 在窗体列表中显示设备名
                foreach (var item in CCameraTool.DeviceInfoList)
                {
                    VisionDesigner.Camera.CDeviceInfo cDeviceInfo = item as VisionDesigner.Camera.CDeviceInfo;
                    if (cDeviceInfo is CGigeDeviceInfo)
                    {
                        CGigeDeviceInfo cGigeDeviceInfo = cDeviceInfo as CGigeDeviceInfo;
                        if (cGigeDeviceInfo.UserDefinedName != "")
                        {
                            cbDeviceList.Items.Add(cGigeDeviceInfo.SerialNumber);
                        }
                        else
                        {
                            cbDeviceList.Items.Add(cGigeDeviceInfo.SerialNumber);
                        }

                    }
                    else if (cDeviceInfo is CUSBDeviceInfo)
                    {
                        CUSBDeviceInfo cUsbDeviceInfo = cDeviceInfo as CUSBDeviceInfo;

                        if (cUsbDeviceInfo.UserDefinedName != "")
                        {
                            cbDeviceList.Items.Add(cUsbDeviceInfo.SerialNumber);
                        }
                        else
                        {
                            cbDeviceList.Items.Add(cUsbDeviceInfo.SerialNumber);
                        }

                    }
                }

                // 选择第一项
                if (m_nDeviceNum != 0)
                {
                    cbDeviceList.SelectedIndex = 0;
                }

            }
            catch (MvdException ex)
            {
                Form1.MyForm.SetLabelText("Fail to enum devices! ErrorCode: 0x" + ex.ErrorCode.ToString("X") + "\r\n");
            }
        }

        private void cbDeviceList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(globalTool!=null)
            {
                globalTool.ModuParams.SetChosenCameraSN(cbDeviceList.Text);
            }
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
           

        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            if (globalTool != null)
            {
                globalTool.ModuParams.Width = (int)numericUpDown1.Value;
            }
        }

        private void numericUpDown2_ValueChanged(object sender, EventArgs e)
        {
            if (globalTool != null)
            {
                globalTool.ModuParams.Height = (int)numericUpDown2.Value;
            }
        }

        private void comboBox2_SelectedValueChanged(object sender, EventArgs e)
        {
            if (this.comboBox2.Text == "Mono 8")
            {
                if (globalTool != null)
                {
                    globalTool.ModuParams.PixelFormat = (int)VMPixelFormat.VM_PIXEL_MONO_08;
                }
            }
                
            else if (this.comboBox2.Text == "RGB 24 C3")
            {
                if (globalTool != null)
                {
                    globalTool.ModuParams.PixelFormat = (int)VMPixelFormat.VM_PIXEL_RGB24_C3;
                }
                
            }
        }

        private void numericUpDown3_ValueChanged(object sender, EventArgs e)
        {
            if (globalTool != null)
            {
                globalTool.ModuParams.ExposureTime = (double)numericUpDown3.Value;
            }
        }

        private void numericUpDown4_ValueChanged(object sender, EventArgs e)
        {
            if (globalTool != null)
            {
                globalTool.ModuParams.Gain = (double)numericUpDown4.Value;
            }
        }
    }
}
